package com.manolo.client.form;

import java.awt.Rectangle;
import javax.swing.JTextArea;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.ScrollPane;

import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import java.awt.ComponentOrientation;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;

import com.manolo.client.connection.Connection;
import com.manolo.event.dispatcher.CardDispatcher;
import com.manolo.event.dispatcher.ChatDispatcher;
import com.manolo.event.dispatcher.DispatchType;
import com.manolo.event.dispatcher.GameDispatcher;
import com.manolo.event.dispatcher.SystemDispatcher;
import com.manolo.event.entity.Card;
import com.manolo.event.HandCardEvent;
import com.manolo.event.ChatEvent;
import com.manolo.event.GameStartEvent;
import com.manolo.event.PlayerListEvent;
import com.manolo.event.SystemEvent;
import com.manolo.event.listener.CardListener;
import com.manolo.event.listener.ChatListener;
import com.manolo.event.listener.GameListener;
import com.manolo.event.listener.SystemListener;
import com.manolo.client.form.TrucoWindow;

import javax.swing.JList;
/**
 * Example of components laid out in a grid
 */
public class MessageWindow extends javax.swing.JFrame implements ChatListener,SystemListener, GameListener{
	private Connection client;  //  @jve:decl-index=0:
	private javax.swing.JPanel ivjJFrameContentPane = null;
	private JTextArea jTextArea = null;
	private JTextField jtxtMsg = null;
	private JButton jbtnSend = null;
	private JScrollPane jspScroll = null;
	private Thread threadWindows;
	private JList jPlayerList = null;
	private DefaultListModel model = new DefaultListModel();
	public MessageWindow(Thread thread) {
		super();
		initialize();
		ChatDispatcher.getInstance().addLocalListener(this);
		SystemDispatcher.getInstance().addLocalListener(this);
		this.threadWindows = thread;
		SystemDispatcher.getInstance().dispatchGetPlayerList(DispatchType.EXTERNAL);
		GameDispatcher.getInstance().addLocalListener(this);
	}
	
	
	
	public void showWindow(Connection client){
		this.client = client;
		super.show();
	}
	
	/**
	 * Return the JFrameContentPane property value.
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJFrameContentPane() {
		if (ivjJFrameContentPane == null) {
			ivjJFrameContentPane = new javax.swing.JPanel();
			ivjJFrameContentPane.setName("JFrameContentPane");
			ivjJFrameContentPane.setLayout(null);
			ivjJFrameContentPane.add(getJTextArea(), null);
			ivjJFrameContentPane.add(getJtxtMsg(), null);
			ivjJFrameContentPane.add(getJbtnSend(), null);
			ivjJFrameContentPane.add(getJScrollPane(), null );
			ivjJFrameContentPane.add(getJPlayerList(), null);
		}
		return ivjJFrameContentPane;
	}

	/**
	 * Initialize the class.
	 */
	private void initialize() {

		this.setName("JFrame1");
		this.setResizable(false);
		this.setSize(new Dimension(673, 500));
		this.setLocation(new Point(45, 25));
		this.setPreferredSize(new Dimension(500, 500));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setTitle("Messages");
		this.setContentPane(getJFrameContentPane());
		this.addWindowListener(new java.awt.event.WindowListener() {  

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				client.close();
				threadWindows.stop();
				//ChatDispatcher.getInstance().removeLocalListener(this);
				
			}
			@Override
			public void windowDeactivated(WindowEvent e) {
				//ChatDispatcher.getInstance().removeListener(this);
				
			}
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(10, 9, 573, 420));
			jTextArea.setEditable(false);
			jTextArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jTextArea.setAutoscrolls(true);
			jTextArea.setLineWrap(true);
			jTextArea.setText("");
		}
		return jTextArea;
	}
	
	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JScrollPane getJScrollPane() {
		if (jspScroll == null) {
			jspScroll = new JScrollPane(this.jTextArea);
			jspScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED );
			jspScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			jspScroll.setBounds(new Rectangle(136, 11, 523, 420));
			jspScroll.setWheelScrollingEnabled(true);
		}
		return jspScroll;
	}

	/**
	 * This method initializes jtxtMsg	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getJtxtMsg() {
		if (jtxtMsg == null) {
			jtxtMsg = new JTextField();
			jtxtMsg.setBounds(new Rectangle(11, 439, 556, 26));
			jtxtMsg.addKeyListener(new java.awt.event.KeyListener() {

				@Override
				public void keyPressed(KeyEvent event) {
					if(event.getKeyCode() == 10)
						addTextToTextArea();
				}

				@Override
				public void keyReleased(KeyEvent event) {
					
				}

				@Override
				public void keyTyped(KeyEvent event) {
					
				}
				
			});
		}
		return jtxtMsg;
	}

	/**
	 * This method initializes jbtnSend	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnSend() {
		if (jbtnSend == null) {
			jbtnSend = new JButton();
			jbtnSend.setBounds(new Rectangle(581, 439, 76, 26));
			jbtnSend.setText("Send");
			jbtnSend.addMouseListener(new java.awt.event.MouseListener() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					addTextToTextArea();
					/*TrucoWindow truco = new TrucoWindow();
					truco.show();*/
				}
				public void mousePressed(java.awt.event.MouseEvent e) {
				}
				public void mouseReleased(java.awt.event.MouseEvent e) {
				}
				public void mouseEntered(java.awt.event.MouseEvent e) {
				}
				public void mouseExited(java.awt.event.MouseEvent e) {
				}
			});
		}
		return jbtnSend;
	}
	private void addTextToTextArea(){
		ChatEvent evt = new ChatEvent();
		evt.setText(getJtxtMsg().getText());
		ChatDispatcher.getInstance().dispatchChatEvent(evt, DispatchType.EXTERNAL);
		getJtxtMsg().setText("");
		getJtxtMsg().requestFocus();
	}
	
	
	
	@Override
	public void GlobalChat(ChatEvent event) {
		if(getJTextArea().getText().equals(""))
			getJTextArea().setText(getJTextArea().getText() + event.getText());
		else
			getJTextArea().setText(getJTextArea().getText() + "\n" + event.getText() );
		getJTextArea().setCaretPosition(getJTextArea().getDocument().getLength());
	}



	/**
	 * This method initializes jPlayerList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJPlayerList() {
		if (jPlayerList == null) {
			jPlayerList = new JList(model);
			jPlayerList.setBounds(new Rectangle(10, 11, 120, 419));
		}
		return jPlayerList;
	}


	@Override
	public void defineName(SystemEvent event) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void nameAccepted(SystemEvent event) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void nameDeclined(SystemEvent event) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void getPlayerList() {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void setPlayerList(PlayerListEvent event) {
		for(String player : event.getPlayerList()) {
			model.addElement(player);
		}
	}



	@Override
	public void playerJoined(PlayerListEvent event) {
		model.addElement(event.getPlayerName());
	}



	@Override
	public void playerLeft(PlayerListEvent event) {
		model.removeElement(event.getPlayerName());
		
	}



	@Override
	public void startGame(GameStartEvent event) {
		this.hide();
		TrucoWindow trucoWindow = new TrucoWindow(event.getBottomPlayerName(), event.getLeftPlayerName(),
												  event.getTopPlayerName(), event.getRightPlayerName());
		trucoWindow.show();
		
	}
	
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
